require(['init'], function () {
    //获取当前月份
   $('#tableSt').css({
      "margin-top":"40px"

    })
    var nowdate = new Date();
    nowdate.setMonth(nowdate.getMonth()-1);
        var y = nowdate.getFullYear();
        var m = nowdate.getMonth()+1;
        var d = nowdate.getDate();
        var formatwdate = +'0'+y+'-'+'0'+m+'-'+d;
   var begintime=$("#beginTime").val(formatwdate);
   var endtime = new Date(new Date() - 1000 * 60*60*24).Format('YYYY-MM-DD');
    $('#endTime').attr('data-value', endtime)
    $('#endTime').val(endtime);
    agencyinformation();
    $('#Thequery1').on('click', function () {
        if ($('#table1').html() == "") {
            agencyinformation(begintime);
        } else {
            $('#table1').html("");
            agencyinformation(begintime);
        }
        $('#tableSt').hide();

    })
   function agencyinformation(begintime,endtime) {
     var begintime = $('#beginTime').val();
     var endtime = $('#endTime').val();
      ajaxMethod({
            url: '/api/coursetypes',
            data: {
                "begintime": begintime,
                "endtime":endtime,

            },
            success: function (json) {
                var p = '<p style="margin:50px 44px">'+i18next.t('table.dataempty')+'</p>'//数据为空
                if ( json== "") {
                    $('#box').html(p)
                } else {
                    formatData(json);
               }
             }
        });
        var $head = '<th style="border-bottom:1px solid #ccc;width:172px">'+i18next.t('table.student')+'</th>';
        var body = '';
        var $tbody=''
		     //课程id数组
        function  formatData(json){
          for(var i=0;i<json.length;i++){
            var coursetypename=json[i].coursetypename;
            var coursetypeuid=json[i].coursetypeuid;
            $head +='<th data-id='+coursetypeuid+' style="border:1px solid #ccc;width:130px;">'+coursetypename+'</th>';
          }
      //表头
      $('#table1').append('<thead><tr class="enroll" style="border:1px solid #ccc;border-bottom:none;width:130px">' + $head + '</tr></thead>');
      //课节
      ajaxMethod({
        url: '/api/statistic/student',
        datatype: 'json',
        data: {
            "begintime": begintime,
            "endtime":endtime,

        },
        type: 'get',
        success:function(data){
          classDats(data);
        }
      });
      function classDats(data){
          //遍历学生
            for(var i=0;i<data.length;i++){
              var coursetypesN=data[i].coursetypes;
              var $tr = $('<tr class="enroll" style="border:1px solid #ccc;border-bottom:none;width:130px" data-uid='+data[i].studentuid+' data-studentname='+data[i].name+'><td><a>'+data[i].name+'</a></td></tr>');
              //学生的详细信息
              $tr.on('click',function(){
                  var tuid=$(this).attr("data-uid");
                  var studentname=$(this).attr("data-studentname");
                  console.log(tuid)
                  window.location.href="student_class.html?tuid=" + tuid + "&studentname="+studentname+"&begintime=" + begintime + "&endtime="+endtime;
              })

      				 for(var k=0;k<json.length;k++){
                var flag=false;
                 //遍历课程与时数
      					 for(var j=0;j<coursetypesN.length;j++){
      						if(!flag){
      							if(json[k].coursetypeuid === coursetypesN[j].id){
                    $tr.append('<td>'+coursetypesN[j].counts+'</td>');
      								flag=true;
                      //break;
                    }
      					}
      				}
              if(!flag){
                $tr.append('<td></td>');
               }
             }
          $("#table1").append($tr);
         }

			}
    }
  }


})
